<template>
  <section>
    <el-form
      :model="loginForm"
      :rules="loginRules"
      label-position="center"
      ref="login"
      class="login-from login-container"
    >
      <h2 class="title">系统登录</h2>
      <el-form-item prop="username">
        <el-input type="text" v-model="loginForm.username" auto-complete="off" placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="loginForm.password" auto-complete="off" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="button"
          @click.native.prevent="subLogin"
          :loading="logining"
          style="width:100%; color:blue "
        >登录</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>
<script>
import { login } from "@/api/user";
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        account: [{ required: true, message: "请输入账号", trigger: "blur" }],
        checkPass: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      logining: false,
    };
  },

  methods: {
    subLogin(ev) {
      this.$refs.login.validate((valid) => {
        if (valid) {
          login(this.loginForm).then((res) => {
            if (res) {
              sessionStorage.setItem("user", res.data);
              //向COOKIE 中写入登录状态
              document.cookie = 1;
              this.$router.push({ path: "/home" });
            } else {
              console.dir(res);
              this.loginForm = [];
            }
          });
        }
      });
    },
  },
};
</script>

<style>
.login-container {
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 18px auto;
  width: 400px;
  padding: 35px 35px 35px 35px;
  background: white;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  text-align: center;
  position: relative;
  top: 100px;
}
.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: yellowgreen;
}
</style>
